//
//  YLYbudgetSubViewController.m
//  iEasyBill
//
//  Created by student on 14-6-3.
//  Copyright (c) 2014年 com.yly16. All rights reserved.
//

#import "YLYbudgetSubViewController.h"
#import "PICircularProgressView.h"

@interface YLYbudgetSubViewController ()<YLYCalculatorViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *keyView;
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet YLYCalculatorView *calculatorView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation YLYbudgetSubViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.rootList = [[NSMutableArray alloc]init];
    self.rootList = [[DatabaseManager shareManager]showBillTypeSubById:[[[self.rootDictionary objectForKey:@"type"] typeID] integerValue]];
    self.calculatorView.delegate = self;
    [self.keyView setHidden:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.rootList.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    
    UIImageView *imageView = (UIImageView*)[cell viewWithTag:1];
    UILabel *nameLanbel = (UILabel *)[cell viewWithTag:2];
    UILabel *budgetLabel = (UILabel*)[cell viewWithTag:4];
    UILabel *shengyuLabel = (UILabel*)[cell viewWithTag:5];
    PICircularProgressView *picView = (PICircularProgressView*)[cell viewWithTag:3];
    float shengyu = 0.0;
    if (indexPath.row == 0) {
        iBillType *type = [self.rootDictionary objectForKey:@"type"];
        imageView.image = [UIImage imageWithData:type.typeImageData];
        nameLanbel.text = type.typeName;
        shengyu = type.budgetMoney.floatValue - [[self.rootDictionary objectForKey:@"money"] floatValue];
        budgetLabel.text = [NSString stringWithFormat:@"¥%.2f",type.budgetMoney.floatValue];
        shengyuLabel.text = [NSString stringWithFormat:@"¥%.2f",shengyu];
        picView.progress = shengyu/type.budgetMoney.floatValue;
        
        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"green.jpg"]];
    }
    else{
        iBillTypeSub *sub = [self.rootList objectAtIndex:indexPath.row - 1];
        imageView.image = [UIImage imageWithData:sub.typeSubImageData];
        nameLanbel.text = sub.typeSubName;
        NSMutableArray *list = [self.rootDictionary objectForKey:@"list"];
        float money = 0.0;
        for (iBill *bill in list) {
            if (bill.billTypeSubID.intValue == sub.typeSubID.intValue) {
                money += bill.billMoney.floatValue;
            }
        }
        shengyu = sub.budgetMoney.floatValue - money;
        budgetLabel.text = [NSString stringWithFormat:@"¥%.2f",sub.budgetMoney.floatValue];
        shengyuLabel.text = [NSString stringWithFormat:@"¥%.2f",shengyu];
        picView.progress = shengyu/sub.budgetMoney.floatValue;
        
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    myIndexPath = indexPath;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //选中后的反显颜色即刻消失 一切操作结束才取消反映色
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.3;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 0.8;
	animation.removedOnCompletion = YES;
    animation.type = @"cube";//
    [self.keyView.layer addAnimation:animation forKey:@"animation"];
    [self.keyView setHidden:NO];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */
//TODO:YLYCalulatorViewDelegate
-(void)showCalculator:(NSString *)calculatorStr{
    self.showLabel.text = calculatorStr;
}
-(void)quitCalculator{
    float sum = self.showLabel.text.floatValue;
    iBillType *type = [self.rootDictionary objectForKey:@"type"];
    if (myIndexPath.row == 0) {
        [[DatabaseManager shareManager]ModifyBillTypeBudgetMoney:type withMoney:sum];
        
    }
    else{
        float key = type.budgetMoney.floatValue + sum;
        iBillTypeSub *sub = [self.rootList objectAtIndex:myIndexPath.row - 1];
        type.budgetMoney = [NSNumber numberWithFloat:key];
        sub.budgetMoney = [NSNumber numberWithFloat:sum];
        [[DatabaseManager shareManager]ModifyBillTypeBudgetMoney:type withMoney:key];
        [[DatabaseManager shareManager]ModifyBillTypeBudgetMoney:sub withMoney:sum];
        
    }
    [self.tableView reloadData];
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.5;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 0.8;
	animation.removedOnCompletion = YES;
    animation.type = @"cube";//
    [self.keyView.layer addAnimation:animation forKey:@"animation"];
    [self.keyView setHidden:YES];
    
}
- (IBAction)chose:(id)sender {
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.5;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 0.8;
	animation.removedOnCompletion = YES;
    animation.type = @"cube";//
    [self.keyView.layer addAnimation:animation forKey:@"animation"];
    [self.keyView setHidden:YES];
}
@end
